# pip install PyMySQL
import pymysql  

host='localhost'
database='my_application'
user='root'
password='Root1234567'
token_lifetime=2592000
# 连接数据库  
connection = pymysql.connect(host=host,  
                             user=user,  
                             password=password,  
                             database='my_application',  
                             charset='utf8mb4',  # 使用utf8mb4编码，以支持emoji等特殊字符  
                             cursorclass=pymysql.cursors.DictCursor)  # 使用字典类型的游标  
  
try:  
    with connection.cursor() as cursor:  
        # 创建一条SQL语句，这里以查询为例  
        sql = 'select * from token where token="ae7959dfa37b2989eda36ab977752c2c" '  
        cursor.execute(sql)  
  
        # 获取所有记录列表  
        # results = cursor.fetchall()  
        results = cursor.fetchone()
  
        # for row in results:  
        #     print(row) 

        if results:
        # result['expired_at'] = time.time() + token_lifetime
            
        # 获取用户信息and expired_at > {expired_at}
            user = {
                'user_id'    : results['user_id'],
                'token_type' : results['token_type']
            }

            print( user      )
finally:  
    connection.close()  # 关闭数据库连接